package com.eelpo.cmmp.uac.dao.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.eelpo.cmmp.core.spring.orm.jpa.BasicCommonDao;
import com.eelpo.cmmp.uac.dao.IEntityManageDao;
import com.eelpo.cmmp.uac.dao.entity.TEntity;
import com.eelpo.cmmp.uac.dao.entity.TPermission;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.common.query.Expression;
import com.eelpo.common.query.Join;
import com.eelpo.common.query.JpaCriteria;
import com.eelpo.common.query.Order;
import com.eelpo.common.query.Result;
import com.eelpo.common.query.criteria.Criteria;

/**
 * <pre>
 * 
 * 业务实体数据访问具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Repository
public class EntityManageDao extends BasicCommonDao implements IEntityManageDao {

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#getEntityById(java.lang.String)
	 */
	@Override
	public TEntity getEntityById(String id) throws DataAccessException {
		return null;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#getEntityByCode(java.lang.String
	 *      )
	 */
	@Override
	public TEntity getEntityByCode(String code) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntity.class);
		criteria.add(Expression.eq("code", code));
		List<?> list = getResultList(criteria);
		return (TEntity) (list.isEmpty() ? null : list.get(0));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#checkExist(java.lang.String)
	 */
	@Override
	public boolean checkExist(String code) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntity.class);
		criteria.result(Result.count());
		criteria.add(Expression.eq("code", code));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#checkExistByUpdate(java.lang
	 *      .String, java.lang.String)
	 */
	@Override
	public boolean checkExistByUpdate(String id, String code) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntity.class);
		criteria.result(Result.count());
		criteria.add(Expression.ne("id", id));
		criteria.add(Expression.eq("code", code));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#addEntity(com.eelpo.cmmp.uac
	 *      .dao.entity.TEntity)
	 */
	@Override
	public String addEntity(TEntity entity) throws DataAccessException {
		persist(entity);
		return entity.getId();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#updateEntity(com.eelpo.cmmp
	 *      .uac.dao.entity.TEntity)
	 */
	@Override
	public void updateEntity(TEntity entity) throws DataAccessException {
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#deleteEntity(java.lang.String)
	 */
	@Override
	public void deleteEntity(String id) throws DataAccessException {
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#getMenuEntities()
	 */
	@Override
	public List<TEntity> getMenuEntities() throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TEntity.class, "e");
		criteria.result(Result.result());
		criteria.join(Join.joinFetch("type t"));
		criteria.join(Join.leftJoinFetch("parent p"));
		criteria.join(Join.leftJoinFetch("parentRelation r"));
		criteria.add(Expression.eqInornate("t.code", "MENU"));
		criteria.add(Expression.eq("status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("t.status", Status.ENABLED.getValue()));
		criteria.order(Order.asc("index"));
		return getResultList(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IEntityManageDao#getMenuEntities(java.lang.String)
	 */
	@Override
	public List<TEntity> getMenuEntities(String userId) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TPermission.class, "p");
		criteria.result(Result.distinct());
		criteria.result(Result.resultInornate("e"));
		criteria.join(Join.join("entity e"));
		criteria.join(Join.join("action a"));
		criteria.join(Join.join("roles r"));
		criteria.join(Join.joinInornate("r.users u"));
		criteria.add(Expression.eqInornate("u.id", userId));
		criteria.add(Expression.eqInornate("a.code", "VIEW"));
		criteria.add(Expression.eqInornate("e.type.code", "MENU"));
		criteria.add(Expression.eq("status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("r.status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("e.status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("a.status", Status.ENABLED.getValue()));
		criteria.add(Expression.eqInornate("u.status", Status.ENABLED.getValue()));
		criteria.order(Order.asc("index"));
		return getResultList(criteria);
	}

}
